package com.songshuai.webapi.baseapi.service.impl;

import com.songshuai.webapi.baseapi.common.settings.CacheKeys;
import com.songshuai.webapi.baseapi.dao.YixueBaseSchoolmasterSourceMapper;
import com.songshuai.webapi.baseapi.dto.feign.response.MasterSourceItemDto;
import com.songshuai.webapi.baseapi.model.YixueBaseSchoolmasterSource;
import com.songshuai.webapi.baseapi.service.SchoolMasterSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: Administrator
 * @Date: 2018/12/26 0026 15:25
 * @Description:
 */
@Service
public class SchoolMasterSourceServiceImpl implements SchoolMasterSourceService {

    @Autowired
    YixueBaseSchoolmasterSourceMapper schoolmasterSourceMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<MasterSourceItemDto> getAllSources() {
        List<MasterSourceItemDto> retList=new ArrayList<>();
        ValueOperations<String,List<MasterSourceItemDto>> opsForValue=redisTemplate.opsForValue();
        retList=opsForValue.get(CacheKeys.MASTER_SOURCE_KEY);
        if(retList==null){
            retList=new ArrayList<>();
        }
        if(retList.size()>0){
            return retList;
        }
        List<YixueBaseSchoolmasterSource>schoolmasterSourceList= schoolmasterSourceMapper.selectAllChannels();
        if(schoolmasterSourceList==null||schoolmasterSourceList.size()==0){
            return retList;
        }
        for(YixueBaseSchoolmasterSource source:schoolmasterSourceList){
            MasterSourceItemDto itemDto=new MasterSourceItemDto();
            itemDto.setSourceId(source.getSourceId());
            itemDto.setChannelName(source.getChannelName());
            retList.add(itemDto);
        }
        opsForValue.set(CacheKeys.MASTER_SOURCE_KEY,retList,30, TimeUnit.MINUTES);
        return retList;
    }

    @Override
    public MasterSourceItemDto getSourceById(Integer id) {
        MasterSourceItemDto retData=null;
        if(id==null||id<=0){
            return retData;
        }
        YixueBaseSchoolmasterSource masterSource=schoolmasterSourceMapper.selectSourceById(id);
        if(masterSource!=null){
            retData=new MasterSourceItemDto();
            retData.setSourceId(masterSource.getSourceId());
            retData.setChannelName(masterSource.getChannelName());
        }
        return retData;
    }

    @Override
    public List<MasterSourceItemDto> getSourceByIds(List<Integer> ids) {
        List<MasterSourceItemDto> retList=new ArrayList<>();
        if(ids==null||ids.size()==0){
            return retList;
        }
        List<YixueBaseSchoolmasterSource> sourceList=schoolmasterSourceMapper.selectSourceByIds(ids);
        if(sourceList!=null&&sourceList.size()>0){
            for (YixueBaseSchoolmasterSource source:sourceList){
                MasterSourceItemDto itemDto=new MasterSourceItemDto();
                itemDto.setSourceId(source.getSourceId());
                itemDto.setChannelName(source.getChannelName());
                retList.add(itemDto);
            }
        }
        return retList;
    }


}
